﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：静态页面管理客户端脚本
//创建人：赵国伟
//生成时间：2012-04-15 22:18:50
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/PagesCommon.aspx";
var ModelOfPage;
var ModelOfParam;
var ModelOfContent;
var PageID = 0;
var SWFUploader = null; //上传控件；(SwfUpload)
var SessionID = "";
var UploadPic = new Array();
var CurPage = null;

///初始化
function Init() {
    PageID = 0;
    UploadPic = new Array();
    CurPage = null;

}

//执行删除
function Delete(id) {
   if(!confirm("系统提示：您确定要删除该条静态页面信息？"))
     return ;
   AjaxGet(AjaxServerUrl + "?Action=DeletePage&PageID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#txtPageName").val() == "") {
        alert("系统提示：请输入页面名称！");
        $("#txtPageName").focus();
        return false;
    }
    if ($("#txtTemplateUrl").val() == "") {
        alert("系统提示：请输入模板页地址！");
        $("#txtTemplateUrl").focus();
        return false;
    }
    if ($("#txtPageUrl").val() == "") {
        alert("系统提示：请输入目标页地址！");
        $("#txtPageUrl").focus();
        return false;
    }
    if ($("#txtViewUrl").val() == "") {
        alert("系统提示：请输入页面访问地址！");
        $("#txtViewUrl").focus();
        return false;
    }
    if ($("#ddlParamNums").val() == "") {
        alert("系统提示：请选择页面参数个数！");
        $("#ddlParamNums").focus();
        return false;
    }

    var counts = parseInt($("#ddlParamNums").val());
    for (var i = 1; i <= counts; ++i) {
        if ($("#txtParamName" + i).val().trim() == "") {
            alert("系统提示：请选择页面参数" + i + "的名称！");
            return false;
        }
    }
    if (!IsNumber($("#txtOrderValue").val())) {
        alert("系统提示：排序值必须为数字！");
        $("#txtOrderValue").focus();
        return false;
    }
    return true;
}

///更新页面内容；
function SaveContent() {
    $("#btnSave").attr("disabled", true);
    var url = AjaxServerUrl + "?Action=SaveContent";
    var arrayParams = new Array();
    var arrayContent = new Array();
    for (var j = 1; j <= CurPage.Params.length; ++j) {
        var p = clone(ModelOfContent);
        p._pageid = CurPage.Page._pageid;
        p._paramno = j;
        arrayParams.push(p);
        if (CurPage.Page._isrichtext)
            arrayContent.push("Content" + j + "=" + escape(GetEditorHTMLContents("txtContent" + j)));
        else
            arrayContent.push("Content" + j + "=" + escape($("#txtContent" + j).val()));
    }

    var postData = "jsonData=" + JSON.stringify(eval(arrayParams));
    postData += "&" + arrayContent.join("&");
    AjaxPostEx(url, "btnSave", postData);  //更新数据；
    $("#btnSave").attr("disabled", false);
}


//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }
    var data = clone(ModelOfPage);
    data.Page._pageid = PageID;
    data.Page._pagename = $("#txtPageName").val();
    data.Page._templateurl = $("#txtTemplateUrl").val();
    data.Page._pageurl = $("#txtPageUrl").val();
    data.Page._viewurl = $("#txtViewUrl").val();
    data.Page._ordervalue = $("#txtOrderValue").val();
    data.Page._status = $("#ddlStatus").val();
    data.Page._remark = $("#txtRemark").val();
    data.Page._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data.Page._lastmodifydate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data.Page._isrichtext = $("#chkIsRichText").attr("checked");
    var counts = parseInt($("#ddlParamNums").val());

    for (var i = 1; i <= counts; ++i) {
        var p = clone(ModelOfParam);
        p._pageid = PageID;
        p._paramname = $("#txtParamName" + i).val();
        p._paramno = i;
        data.Params.push(p);
    }
    var url = AjaxServerUrl + "?Action=SavePage";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定静态页面信息到录入表单中；
function BindPageToForm() {
    var url = AjaxServerUrl + "?action=GetPageAndParam&PageID=" + PageID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#txtPageName").val(data.Page._pagename);
                $("#txtTemplateUrl").val(data.Page._templateurl);
                $("#txtPageUrl").val(data.Page._pageurl);
                $("#txtViewUrl").val(data.Page._viewurl);
                $("#txtOrderValue").val(data.Page._ordervalue);
                $("#ddlStatus").val(data.Page._status);
                $("#txtRemark").val(data.Page._remark);
                $("#chkIsRichText").attr("checked", data.Page._isrichtext)
                $("#ddlParamNums").val(data.Params.length);
                GenerateParamTable(data.Params.length);
                for (var i = 0; i < data.Params.length; ++i) {
                    $("#txtParamName" + (i + 1)).val(data.Params[i]._paramname);
                }
            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(600, 800, "#e2e2e2", "添加静态页面", GetHtmlForm());
}

//编辑界面；
function Edit(id) {
    Init();
    PageID = id;
    GetPopupDiv(600, 800, "#e2e2e2", "编辑静态页面", GetHtmlForm());
    BindPageToForm();
}

///编辑页面内容；
function EditContent(id) {
    Init();
    PageID = id;
    var url = AjaxServerUrl + "?action=GetPage&PageID=" + PageID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                CurPage = data;
                GetPopupDiv(750, 800, "#e2e2e2", "编辑页面内容", GetContentHTML(data));
                var c = new HashTable();
                if (data.Contents != null) {
                    for (var i = 0; i < data.Contents.length; ++i) {
                        c.add(data.Contents[i]._paramno, data.Contents[i]._paramcontent);
                    }
                }
                for (var i = 0; i < data.Params.length; ++i) {
                    if (data.Page._isrichtext)
                        GetEditor("txtContent" + (i + 1), "98%");
                    $("#txtContent" + (i + 1)).val(c.items2((i + 1)))
                }
                SWFUploader = CreateSwfUploadObject("/Manage/AS/AjaxUpload.aspx?action=UploadFiles", SessionID, "*.jpg;*.gif;*.png", "图片文件", "spUploadBtn", "divUploadPic");
            }
        }
    });
}

function InsertPic(bianhao,paramNo) {
    var html = "<img src=\"" + UploadPic[bianhao] + "\" > ";
    InsertEditorHTML("txtContent" + (paramNo), html);
}

function DeleteUploadPic(bianhao) {
    if (!confirm("系统提示：确定要删除该图片？"))
        return;
    UploadPic.removeEx(bianhao);
    OutUploadPic();
}

function OutUploadPic() {
    if (UploadPic == null || UploadPic.length == 0) {
        $("#trUploadPic").hide();
        $("#divUploadPic").html("");
    }
    else {
        $("#trUploadPic").show();
        var html = new Array();
        html.push("<table  border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >");
        html.push("  <tr>");
        for (var i = 0; i < UploadPic.length; ++i) {
            html.push("    <td><table  border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
            html.push("      <tr>");
            html.push("        <td ><a href=\"javascript:void(0);\" ><img src=\"" + UploadPic[i] + "\" width=\"80\" ></a></td>");
            html.push("<td align=center>");
            for (var j = 0; j < CurPage.Params.length; ++j) {
                html.push("[<a href=\"javascript:InsertPic("+i+","+(j+1)+")\" class=\"green\">加入" + CurPage.Params[j]._paramname + "</a>]<br>");
            }
            html.push("		[<a href=\"javascript:void(0);\" class=\"red\" onclick=\"DeleteUploadPic(" + i + ");\">删除</a>] </td>");
            html.push("</td>");
            html.push("      </tr>");
            html.push("    </table></td>");

        }
        html.push("  </tr>");
        html.push("</table>");
        $("#divUploadPic").html(html.join(""));
    }
}
function uploadSuccess(file, fileUrl) {
    try {
        UploadPic.push(fileUrl);
        OutUploadPic();
    } catch (ex) {
        this.debug(ex);
    }
}


function GetContentHTML(data) {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;编辑页面内容</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"82\" align=\"right\">页面名称：</td>");
    html.push("    <td width=\"668\"><span class=\"blue bold\">" + data.Page._pagename + "</span> [ <a href=\"" + data.Page._viewurl + "\" class=\"green\" target=\"_blank\">查看该页面</a> ] </td>");
    html.push("  </tr>");
    html.push("  <tr >");
    html.push("    <td align=\"right\">图片上传：</td>");
    html.push("    <td><span id=\"spUploadBtn\"></span>(*)</td>");
    html.push("  </tr>");
    html.push("  <tr id=\"trUploadPic\" style=\"display:none\">");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><div  style=\"width:650px;overflow-y:hidden;overflow-x:auto; \" id=\"divUploadPic\"></div></td>");
    html.push("  </tr>");
    html.push("<input type=\"hidden\" id=\"hdParamNum\" value=\"" + data.Params.length + "\"");
    for (var i = 0; i < data.Params.length; ++i) {
        html.push("  <tr>");
        html.push("    <td align=\"right\">" + data.Params[i]._paramname + "：</td>");
        html.push("    <td><textarea name=\"txtContent" + (i + 1) + "\" cols=\"80\" id=\"txtContent" + (i + 1) + "\" rows=\"15\"></textarea></td>");
        html.push("  </tr>");
    }

    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"SaveContent();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");
    return html.join("");
}




///生成输入参数的Table界面代码；
function GenerateParamTable(paramNums) {
    $("#trParams").hide();
    if (paramNums == "") {
        $("#spParams").html("");
        return;
    }
    var counts = parseInt(paramNums);
    var html = new Array();
    html.push("    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
    for (var i = 1; i <= counts; ++i) {
        html.push("      <tr>");
        html.push("        <td width=\"15%\" align=\"right\">参数" + i + "名称：</td>");
        html.push("        <td width=\"85%\"><input name=\"txtParamName" + i + "\" type=\"text\"  class=\"border6\" id=\"txtParamName" + i + "\" size=\"25\" />");
        html.push("          <span class=\"red\">(*)</span></td>");
        html.push("      </tr>");
    }
    html.push("    </table>");
    $("#spParams").html(html.join(""));
    $("#trParams").show();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;页面信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"104\" align=\"right\">页面名称：</td>");
    html.push("    <td width=\"496\"><input name=\"txtPageName\" type=\"text\"  class=\"border6\" id=\"txtPageName\" size=\"25\">");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">模板页地址：</td>");
    html.push("    <td>/Templates/<input name=\"txtTemplateUrl\" type=\"text\"  class=\"border6\" id=\"txtTemplateUrl\" size=\"60\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">目标文件地址：</td>");
    html.push("    <td>/<input name=\"txtPageUrl\" type=\"text\"  class=\"border6\" id=\"txtPageUrl\" size=\"70\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">访问地址：</td>");
    html.push("    <td>/<input name=\"txtViewUrl\" type=\"text\"  class=\"border6\" id=\"txtViewUrl\" size=\"70\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");

    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><label for=\"chkIsRichText\"><input type=\"checkbox\" name=\"checkbox\" value=\"\" id=\"chkIsRichText\" />是否采用富文本框</label> ");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");


    html.push("  <tr>");
    html.push("    <td align=\"right\">页面参数个数：</td>");
    html.push("    <td><select name=\"ddlParamNums\" id=\"ddlParamNums\"  onchange=\"GenerateParamTable(this.options[this.selectedIndex].value)\">");
    html.push("      <option value=\"\">--请选择参数个数--</option>");
    for (var i = 1; i <= 5; ++i) {
        html.push("      <option value=\"" + i + "\">" + i + "个</option>");
    }
    html.push("    </select>    </td>");
    html.push("  </tr>");


    html.push("  <tr id=\"trParams\" style=\"display:none;\">");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><span id=\"spParams\">");
    html.push("</span></td>");
    html.push("  </tr>");


    html.push("  <tr>");
    html.push("    <td align=\"right\">排序值：</td>");
    html.push("    <td><input name=\"txtOrderValue\" type=\"text\" class=\"border6\" id=\"txtOrderValue\" value=\"0\" size=\"5\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">备注：</td>");
    html.push("    <td><textarea name=\"txtRemark\" cols=\"70\" rows=\"3\" class=\"border6\" id=\"txtRemark\"></textarea></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">状态：</td>");
    html.push("    <td><select name=\"ddlStatus\" id=\"ddlStatus\">");
    html.push("	 <option value=\"正用\">正用</option>");
    html.push("	  <option value=\"停用\">停用</option>");
    html.push("    </select>    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}







function yesOther(t) {
    if (SWFUploader != null) {
        SWFUploader.destroy();
    }
    var IsHavePopDiv = false;
    var lastDivName = "";
    var divs = document.getElementsByTagName("DIV");
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0 && divs[i].id != fullPopDivName) {
            oo(divs[i].id).style.display = "";
            IsHavePopDiv = true;
        }
    }
    if (!t) {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    else {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
}





